﻿using CaptchaGen;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ZSZ.Common;
using ZSZ.CommonMvc;
using ZSZ.Iservice;
using ZSZ.QianWeb.App_Start;
using ZSZ.QianWeb.Models.Main;

namespace ZSZ.QianWeb.Controllers
{
    public class MainController : Controller
    {
        public IUserService UserService { set; get; }
        public ISettingService SettingService{ get; set; }
        public ICityService CityService { get; set; }
        // GET: Main
        public ActionResult Index()
        {
            long cityId = (long)QianHelper.GetCityId(HttpContext);
            var cities = CityService.GetAll();
            MainIndexGet model = new MainIndexGet();
            model.Cities = cities;
            model.CityName = CityService.GetById(cityId).Name;
            return View(model);
        }
        [HttpPost]
        public ActionResult ChangeCity(long id) {
            var city = CityService.GetById(id);
            var userId = QianHelper.GetUserId(HttpContext);
            if (userId!=null)
            {
                UserService.SetUserCityId((long)userId,id);
            }
            return Json(new JsonCommon { Status="ok",Data=city});
        }
        [HttpGet]
        public ActionResult Register()
        {
            return View();
        }
        [HttpGet]
        public ActionResult CreateYan()
        {
            string yan = CommonHelper.GenerateCaptchaCode(4);
            MemoryStream ms = ImageFactory.GenerateImage(yan, 60, 100, 20, 6);
            TempData["yan"] = yan;
            return File(ms, "image/jpeg");
        }
        [HttpPost]
        public ActionResult SendDuanXin(string yan, string shou)
        {
            if (!(yan == TempData["yan"].ToString()))
            {
                return Json(new JsonCommon { Status = "err", ErrorMsg = "验证码错误" });
            }
            string phoneyan = new Random().Next(1000, 9999).ToString();
            JiSuSmsSend send = new JiSuSmsSend();
            send.Appkey = SettingService.GetValue("急速数据appkey");
            send.Content = SettingService.GetValue("急速数据content模板").Replace("@",phoneyan);
            JiSuSmsResult result = send.SendSms(shou);
            if (result.Status=="0")
            {
                TempData["Phone"] = shou;
                TempData["PhoneYan"] = phoneyan;
                return Json(new JsonCommon { Status = "ok" });
            }
            return Json(new JsonCommon { Status="err",ErrorMsg=result.Msg});
        }
        [HttpPost]
        public ActionResult Reister(RegisterPostModel model)
        {
            if (!ModelState.IsValid)
            {
                string err = ModelStateConsole.GetErrorMsg(ModelState);
                return Json(new JsonCommon { Status = "err", ErrorMsg = err });
            }
            if (!(TempData["Phone"].ToString() == model.Phone && model.PhoneYan == TempData["PhoneYan"].ToString()))
            {
                return Json(new JsonCommon { Status = "err", ErrorMsg = "您输入的验证码或者手机号不是之前的手机号" });
            }
            long id = UserService.AddNew(model.Phone, model.Pass);
            if (id < 1)
            {
                return Json(new JsonCommon { Status = "err", ErrorMsg = "注册失败，请联系管理员" });
            }
            return Json(new JsonCommon { Status = "ok" });
        }
    }
}